package com.vincent.springbucks;

import com.vincent.springbucks.entity.Coffee;
import com.vincent.springbucks.entity.CoffeeOrder;
import com.vincent.springbucks.entity.OrderState;
import com.vincent.springbucks.repository.CoffeeRepository;
import com.vincent.springbucks.repository.CofferOrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Sort;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication
@Slf4j
public class SpringbucksApplication implements CommandLineRunner {
    @Autowired
    private CoffeeRepository coffeeRepository;
    @Autowired
    private CofferOrderRepository cofferOrderRepository;

    public static void main(String[] args) {
        SpringApplication.run(SpringbucksApplication.class, args);
    }

    @Override
    @Transactional
    public void run(String... args) throws Exception {
//        initOraders();
        findOrders();
    }

    private void findOrders() {
        // 获取全部，支持排序
        coffeeRepository.findAll(Sort.by(Sort.Direction.ASC, "id"))
                .forEach(c -> log.info("load coffee -> {}", c));

        // 获取top3
        List<CoffeeOrder> top3 = cofferOrderRepository.findTop3ByOrderByUpdateTimeDescIdAsc();
        log.info("top3 -> {}", getJoinedOrderId(top3));

        // hibernate懒加载，获取关联信息时才去入库，因此需要开启事务Transactional
        List<CoffeeOrder> orders = cofferOrderRepository.findByCustomerOrderById("vincent");
        orders.forEach(o -> o.getItems().forEach(coffee -> log.info("vincent order item -> {}", coffee)));

        // 通过咖啡名称获取订单信息
        List<CoffeeOrder> latteOrders = cofferOrderRepository.findByItems_Name("latte");
        log.info("latte orders -> {}", getJoinedOrderId(latteOrders));

    }

    private String getJoinedOrderId(List<CoffeeOrder> list) {
        for (CoffeeOrder coffeeOrder : list) {
            log.info(coffeeOrder.toString());
        }
        return list.stream().map(o -> o.getId().toString())
                .collect(Collectors.joining(","));
    }

    private void initOraders() {
        // 首先添加两种咖啡，拿铁和浓缩咖啡
        Coffee latte = Coffee.builder().name("latte")
                .price(Money.of(CurrencyUnit.of("CNY"), 28.8))
                .build();
        coffeeRepository.save(latte);
        log.info("coffee -> {}",latte);
        Coffee espresso = Coffee.builder().name("espresso")
                .price(Money.of(CurrencyUnit.of("CNY"), 32.8))
                .build();
        coffeeRepository.save(espresso);
        log.info("coffee -> {}",espresso);

        // 添加两个订单
        CoffeeOrder vincent = CoffeeOrder.builder().customer("vincent")
                .items(Collections.singletonList(latte))
                .state(OrderState.INIT)
                .build();
        cofferOrderRepository.save(vincent);
        log.info("order -> {}",vincent);
        CoffeeOrder john = CoffeeOrder.builder().customer("john")
                .items(Arrays.asList(latte,espresso))
                .state(OrderState.PAID)
                .build();
        cofferOrderRepository.save(john);
        log.info("order -> {}",john);
    }
}
